/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.bfpro.blocksolution;


/**
 *
 * @author fbn
 */
public class BlockSolution {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Placement place = new Placement(5,5); // кол-во столбцов, кол-во строк
        Placement_Cell[][] cells = place.getCell();
        
        Figure[] figures = new Figure[7];
        /**
         * Создание фигур (номер, кол-во столбцов, кол-во строк)
         */
        Figure f1 = new Figure(1,3,2);
        int[][] pcs1 = {{1,0,1},{1,1,1}};
        f1.setPcs(pcs1);

        Figure f2 = new Figure(2,1,2);
        int[][] pcs2 = {{1},{1}};
        f2.setPcs(pcs2);

        Figure f3 = new Figure(3,1,3);
        int[][] pcs3 = {{1},{1},{1}};
        f3.setPcs(pcs3);

        Figure f4 = new Figure(4,2,2);
        int[][] pcs4 = {{1,0},{1,1}};
        f4.setPcs(pcs4);
        
        Figure f5 = new Figure(5,1,4);
        int[][] pcs5 = {{1},{1},{1},{1}};
        f5.setPcs(pcs5);
        
        Figure f6 = new Figure(6,2,3);
        int[][] pcs6 = {{1,1},{1,0},{1,0}};
        f6.setPcs(pcs6);
        
        Figure f7 = new Figure(7,2,3);
        int[][] pcs7 = {{0,1},{1,1},{1,0}};
        f7.setPcs(pcs7);
        
        f1.Paint();
        f2.Paint();
        f3.Paint();
        f4.Paint();
        f5.Paint();
        f6.Paint();
        f7.Paint();
        
        /**
         * Массив фигур
         */        
        figures[0]=f1;
        figures[1]=f2;
        figures[2]=f3;
        figures[3]=f4;
        figures[4]=f5;
        figures[5]=f6;
        figures[6]=f7;

        System.out.println("Figures weight before sort :");
        for (int i=0; i<figures.length; i++) {
            System.out.println(i + ". Figure:"+ figures[i].getNumber() +". Weight:" + figures[i].getWeight());
        }
        /**
         *  Массив очередности по размеру фигур
         */
        int[] figurePlaced = new int[7];
        /**
         * Сортировка фигур по размерности (по убыванию)
         */
        for (int i=0; i<figures.length-1; i++) {
            for (int j=i+1; j<figures.length;j++) {
                if (figures[i].getWeight() < figures[j].getWeight()) {
                    Figure f_big = figures[j];
                    Figure f_small = figures[i];
                    figures[i]=f_big;
                    figures[j]=f_small;
                }
            }
        }
        System.out.println("Figures weight after sort :");
        for (int i=0; i<figures.length; i++) {
            System.out.println(i + ". Figure:"+ figures[i].getNumber() +". Weight:" + figures[i].getWeight());
        }
        
        int figure_placed = 0;
        int placed_x = 0;
        int placed_y = 0;
        int fig_placed_x = 0;
        int fig_placed_y = 0;
        int count = 1;
        
        while (figure_placed < figures.length) {
            boolean placed = false;
            for (int y=placed_y;y<place.getY();y++){
                for (int x=placed_x;x<place.getX();x++) {
                    if (place.FigureCheck(figures[figure_placed], x, y)) {
                        System.out.println("Figure: "+ figures[figure_placed].getNumber() +", placed on (x,y): " + x + "," + y + " - OK");
                        place.FigureAdd(figures[figure_placed], x, y);                    
                        placed_x = 0;
                        placed_y = 0;
                        x=place.getX();
                        y=place.getY();
                        figure_placed++;
                        place.Paint();
                        placed = true;
                        System.out.println("Variant:" + count);
                        count++;
                    } else {
                        System.out.println("Figure: "+ figures[figure_placed].getNumber() +", placed on (x,y): " + x + "," + y + " - ERROR");
                        placed = false;
                        if (placed_x > 0) placed_x = 0; 
                    }
                }
            }
            /**
             * Хоть одну фигуру не установили до конца перебора всех ячеек поля
             */
            if (!placed) {
                System.out.println("Очередная фигура не установлена. Меняем план ...");    
                /**
                 * Вычистить место под последней установленной фигурой
                 */
                figure_placed--;
                // Координаты первой ячейки последней установленной фигуры
                fig_placed_x = figures[figure_placed].getCell_place()[0][0].place_x;
                fig_placed_y = figures[figure_placed].getCell_place()[0][0].place_y;
                place.FigureDelete(figures[figure_placed]);
                place.Paint();
                placed_x = fig_placed_x + 1;
                placed_y = fig_placed_y;
                if (placed_x>place.getX()) {
                    placed_x = 0;
                    placed_y = fig_placed_y + 1;
                    if (placed_y>place.getY()) {
                    /**
                     * Вычистить место перед предыдущей фигурой
                     */
                        placed_y = 0;
                        figure_placed--;
                        place.FigureDelete(figures[figure_placed]);
                        place.Paint();
                    }
                }
            }
                    
        }
    }
    
}
